package com.miage.m1.xml.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.miage.m1.xml.Club;
import com.miage.m1.xml.ClubReader;
import com.miage.m1.xml.ClubRepositoryMySQL;
import com.miage.m1.xml.ClubSource;
import com.miage.m1.xml.interfaces.IClubRepository;
import com.miage.m1.xml.test.ClubRepositoryTest;

/**
 * Servlet implementation class ClubInfo
 */
public class ClubInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected static final String _presidentXSLT = "presidentStylesheetXslt";
	protected static final String _coachXSLT = "coachStylesheetXslt";
	protected static final String _defaultXSLT = "defaultStylesheetXslt";

	private IClubRepository clubRepository;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ClubInfo(IClubRepository clubRepository) {
		super();
		this.clubRepository = clubRepository;
	}

	public ClubInfo(){
		this(new ClubRepositoryMySQL());
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().setAttribute("userType", request.getParameter("userType"));
		processRequest(request, response);

	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		try {
			String xslt;

			String userType = (String) request.getSession().getAttribute("userType");
			if(userType.equals("president"))
				xslt = _presidentXSLT;
			else if (userType.equals("coach"))
				xslt =  _coachXSLT;
			else
				xslt = _defaultXSLT;

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			XMLReader clubReader = new ClubReader();
			InputSource clubSource = new ClubSource(this.getClubRepository().findAll()); 
			Source source = new SAXSource(clubReader, clubSource);

			TransformerFactory tFactory = TransformerFactory.newInstance();
			ServletContext webApp = this.getServletContext();
			Result result = new StreamResult(out);

			String filePath = webApp.getRealPath(this.getInitParameter(xslt));//TODO dynamique choix stylesheet
			Transformer transformer = tFactory.newTransformer(new StreamSource(filePath));
			transformer.setParameter("rootPath", request.getContextPath());
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

			// Transformation
			transformer.transform(source, result);

			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public IClubRepository getClubRepository() {
		return clubRepository;
	}
}
